package com.xuecheng.content.api;

import com.spring4all.swagger.EnableSwagger2Doc;
import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.service.CourseBaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author shiminghui
 * @date 2024/7/30 11:06
 * @description: 管理相关接口
 */
@EnableSwagger2Doc //生成swagger接口文档
@RestController
@Slf4j
@RequiredArgsConstructor
@Api(tags = "课程信息管理接口")
public class CourseBaseInfoController {

    private final CourseBaseInfoService courseBaseInfoService;

    @ApiOperation("课程查询接口")
    @PostMapping("/course/list")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto) {
        //在令牌中获取
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        String companyId = user.getCompanyId();
        //

        PageResult<CourseBase> pageResult = courseBaseInfoService.queryCourseBaseList(companyId, pageParams, queryCourseParamsDto);
        return pageResult;
    }


    @ApiOperation("新增课程")
    @PostMapping("/course")
    //@validated注解是激活属性校验,校验的逻辑再类中
    public CourseBaseInfoDto addCourse(@RequestBody @Validated(ValidationGroups.Insert.class) AddCourseDto addCourseDto) {
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.addCourse(1232141425L, addCourseDto);
        return courseBaseInfoDto;
    }


    @ApiOperation("根据id查询课程信息")
    @GetMapping("/course/{id}")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public CourseBaseInfoDto getCourseById(@PathVariable Long id) {
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        log.info("{}", user);
        log.info("根据id查询课程信息,id为{}", id);
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.getCourseBaseInfo(id);
        return courseBaseInfoDto;
    }

    @ApiOperation("修改课程信息")
    @PutMapping("/course")
    public CourseBaseInfoDto updateCourseBaseInfo(@RequestBody @Validated(ValidationGroups.Update.class) EditCourseDto editCourseDto) {
        log.info("修改课程信息的参数为{}", editCourseDto);
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.updateCourse(1232141425L, editCourseDto);
        return courseBaseInfoDto;
    }

    @ApiOperation("删除课程信息")
    @DeleteMapping("/course/{id}")
    public void deleteCourseBaseInfo(@PathVariable Long id) {
        log.info("删除课程信息,id为{}", id);
        courseBaseInfoService.deleteCourseBaseInfo(id);
    }

}
